package Q5;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;

public class ClientThread implements Runnable {

    private Socket client;

    public ClientThread(Socket client) {
        this.client = client;
    }

    @Override
    public void run() {
        ObjectInputStream receive = null;
        //利用线程来收信息
        try {
            receive = new ObjectInputStream(client.getInputStream());
            while (true){
                Message message = (Message) receive.readObject();
                System.out.println("已收到服务器发来的消息");
                System.out.println("收到来自客户端 "+ message.getName()+
                        "发来的消息："+ message.getContent() + "\n发来的文件为："+ message.getFileName());
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            if(null != receive){
                try {
                    receive.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if( null != client){
                try {
                    client.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
